<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md9">
            <div class="layui-form layuimini-form"  enctype="multipart/form-data" method="post" >

                <div class="layui-form-item">
                    <label class="layui-form-label required">请输入手机号</label>
                    <div class="layui-input-block">
                        <input type="number" name="username" id="username" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
                        <span id="phoneSpan"></span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">邮箱</label>
                    <div class="layui-input-block">
                        <input type="email" name="email" placeholder="请输入邮箱" lay-verify="required" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">请输入失效时间</label>
                    <div class="layui-input-block">
                        <input type="text" id="validTime" name="validTime"  placeholder="请输入失效时间" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label required">请输入offer内容</label>
                    <div class="layui-input-block">
                        <textarea name="content" class="layui-textarea" lay-verify="required" placeholder="请输入offer内容"></textarea>
                    </div>
                </div>

<!--                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">请选择offer文件</label>
                    <div class="layui-input-block">
                     &lt;!&ndash;   <button class="layui-btn test" id="fileImage" lay-data="{url: '/a/'}">上传图片</button>&ndash;&gt;
&lt;!&ndash;                        <button class="layui-btn test"  lay-data="{url: '/b/', accept: 'file'}">上传文件</button>&ndash;&gt;
&lt;!&ndash;                         <input type="file" id="file" name="offerFile" class="layui-input" >&ndash;&gt;
&lt;!&ndash;                        <button type="button" class="layui-btn" id="fileUp"><i class="layui-icon"></i>上传文件</button>&ndash;&gt;
                        <button type="button" class="layui-btn layui-btn-normal" id="test8" name="offerFile">选择文件</button>
                        <button type="button" class="layui-btn" id="test9">开始上传</button>
                    </div>
                </div>-->

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveBtn" id="save">确认保存</button>
                        <button class="layui-btn"  id="sendBtn">返回</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../js/commons.js"></script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','laydate','upload'], function () {
        var form = layui.form;
        layer = layui.layer;
        $ = layui.$;
        var upload = layui.upload; //得到 upload 对象
        var laydate = layui.laydate;


        //日期时间选择器
        laydate.render({
            elem: '#validTime',
            type: 'datetime'
        });

        /*        //创建一个上传组件
        var uploadInst = upload.render({
             elem: '.test'     /!*根据绑定id，打开本地图片*!/
            ,url:URL+"file/upload"  /!*上传后台接受接口*!/
            ,auto: false        /!*true为选中图片直接提交，false为不提交根据bindAction属性上的id提交*!/
            ,bindAction: '#get'
            ,drag:true
            ,auto: false
            ,choose:function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#photo').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });*/
        var upload = layui.upload;

       //指定允许上传的文件类型
/*        upload.render({
            elem: '#test8'
            , url: URL + 'recruiter/offer/addOffer' //上传接口
            ,auto: false
            , accept: 'file' //普通文件
            //,multiple: true
            ,bindAction: '#save'
            ,done: function(res){
                layer.msg('上传成功');
                console.log(res)
            }
        });*/


        /**
         * 监听username输入框，查询是否有这个user
         */
        $("#username").on("blur", function () {
            $.ajax({
                url: URL + "user/selectUserByUsername",
                type: "post",
                headers: {"token": localStorage.getItem("token")},
                data: {"username": $("#username").val()},
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg(result.msg);
                    } else {
                        layer.msg(result.msg);
                    }

                }
            })
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            layer.confirm('确认添加吗？', function (index) {
                console.log(data.field);
                $.ajax({
                    url: URL + "recruiter/offer/addOffer",
                    type: "post",
                    headers: {"token": localStorage.getItem("token")},
                    data: data.field,
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("添加成功", function () {
                                //执行搜索重载
                                table.reload('currentTableId', {
                                    page: {
                                        curr: 1
                                    }
                                }, 'data');
                            })
                            window.location.href = "../../html/offer/table.html";
                        } else {
                            layer.msg("添加失败");
                        }
                        layer.close(index);
                        /*                window.location.href="../../html/table.html"*/
                    }
                })
            });
            return false;
        });

        //返回
        $("#sendBtn").on("click", function () {
            window.location.href = "../../html/offer/table.html"
            return false;
        })
    })

</script>
</body>
</html>